import store2, { type StoreType } from 'store2';

type Value = Record<string, any>;

export class Storage {
  private store: StoreType;
  constructor(namespace = '@@global') {
    this.store = store2.namespace(namespace);
  }

  public clear = () => {
    return this.store.clear();
  }

  public setItem = (key: string, value: Value) => {
    const json = JSON.stringify(value);
    return this.store.set(key, json);
  }

  public getItem = <T = any>(key: string) => {
    const result = this.store.get(key);
    if (result) return JSON.parse(result) as T;
  }

  public removeItem = (key: string) => {
    return this.store.remove(key);
  }
}